﻿Imports System.Data.OleDb

Public Class MultipleChoiceAnswerModel
    Inherits ModelWithGUID

    Private _sequence As Integer
    Private _answer As String
    Private _correct As Boolean
    Private _questionGUID As String
    Private _question As MultipleChoiceQuestionModel

    Public ReadOnly Property Sequence As Integer
        Get
            Return _sequence
        End Get
    End Property

    Public ReadOnly Property HasSequence As Boolean
        Get
            Return Sequence <> -1
        End Get
    End Property

    Public ReadOnly Property Answer As String
        Get
            Return _answer
        End Get
    End Property

    Public ReadOnly Property HasAnswer As Boolean
        Get
            Return _answer <> ""
        End Get
    End Property

    Public ReadOnly Property Correct As Boolean
        Get
            Return _correct
        End Get
    End Property

    Public ReadOnly Property QuestionGUID As String
        Get
            Return _questionGUID
        End Get
    End Property

    Public ReadOnly Property HasQuestion As Boolean
        Get
            Return _questionGUID <> ""
        End Get
    End Property

    Public ReadOnly Property Question As QuestionModel
        Get
            If HasQuestion AndAlso IsNothing(_question) Then
                _question = New MultipleChoiceQuestionModel(QuestionGUID)
            End If
            Return _question
        End Get
    End Property

    Protected Overrides Sub CreateBlank()
        _sequence = -1
        _answer = ""
        _correct = False
        _questionGUID = ""
        _question = Nothing
    End Sub

    Private Sub InitialiseFromDataReader(ByVal dr As OleDbDataReader)
        GetField(_sequence, dr, "AnswerSequence", -1)
        GetField(_answer, dr, "Answer", "")
        GetField(_correct, dr, "Correct", False)
        GetField(_questionGUID, dr, "QuestionGUID", "")
        _question = Nothing
    End Sub

    Protected Overrides Function Load() As Boolean
        Dim IsInDB As Boolean
        Dim sqlString As String = "SELECT AnswerSequence, Answer, Correct, QuestionGUID FROM multipleChoiceAnswers WHERE GUID=@GUID"
        Dim oleDbConn As OleDbConnection = db()
        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@GUID", GUID)
        Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
        IsInDB = dataReader.HasRows
        If IsInDB Then
            dataReader.Read()
            InitialiseFromDataReader(dataReader)
        End If
        oleDbConn.Close()
        Return IsInDB
    End Function

    Public Overrides Function Save() As Boolean
        If IsSaved Then
            Return True
        End If
        Dim sqlString As String
        Dim cmd As OleDbCommand
        Dim rowsAffected As Integer
        Dim oleDbConn As OleDbConnection = db()
        If IsInDB Then
            sqlString = "UPDATE multipleChoiceAnswers SET Sequence=@Sequence, Answer=@Answer, Correct=@Correct, QuestionGUID=@QuestionGUID WHERE GUID=@GUID"
        Else
            sqlString = "INSERT INTO multipleChoiceAnswers (Sequence, Answer, Correct, QuestionGUID, GUID) VALUES (@Sequence, @Answer, @Correct, @QuestionGUID, @GUID)"
        End If
        cmd = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@Sequence", If(HasSequence, Sequence, DBNull.Value))
        cmd.Parameters.AddWithValue("@Question", If(HasAnswer, Answer, DBNull.Value))
        cmd.Parameters.AddWithValue("@Correct", Correct)
        cmd.Parameters.AddWithValue("@QuestionGUID", If(HasQuestion, QuestionGUID, DBNull.Value))
        cmd.Parameters.AddWithValue("@GUID", GUID)
        rowsAffected = cmd.ExecuteNonQuery()
        oleDbConn.Close()
        If rowsAffected = 1 Then
            IsInDB = True
            _isSaved = True
            Return True
        Else
            Return False
        End If

    End Function

    Public Sub New(ByVal dr As OleDbDataReader)
        MyBase.New(dr("AnswerGUID"), True)
        InitialiseFromDataReader(dr)
        _isSaved = True
        IsInDB = True
    End Sub

End Class
